package com.css.apps.base.user.action;

import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.user.model.SUser;
import com.css.apps.base.user.service.UserService;
import com.css.core.configuration.Environment;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.opensymphony.xwork.Action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@SuppressWarnings("serial")
public class UpdUserOrder extends UserAction {

	private static Log log = LogFactory.getLog(UpdUserOrder.class);
	private String userId;
	private String deptId;
	private String orderNum;

	@SuppressWarnings("unchecked")
	protected String userGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(userId)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			SUser item = QueryCache.get(SUser.class,userId);
			String defaultOrgOrderNum = "1";
			Integer orgOrderNum = QueryCache.get(SOrg.class,deptId).getOrderNum();
			if(orgOrderNum != null){
				defaultOrgOrderNum = String.valueOf(orgOrderNum);
			}
			item.setOrderNum(UserService.toFullNum(defaultOrgOrderNum,orderNum));
			tx = new TransactionCache();
			tx.update(item);
			tx.commit();
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			log.error(ex.getMessage(), ex);
			return Action.ERROR;
		}
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getOrderNum() {
		return orderNum;
	}

	public void setOrderNum(String orderNum) {
		this.orderNum = orderNum;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}
}
